package cn.caplike.demo.google.protobuf.with_netty_multi;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

/**
 * Description:
 *
 * @author LiKe
 * @version 1.0.0
 * @date 2021-12-01 13:15
 */
public class TestClientChannelInboundHandler extends SimpleChannelInboundHandler<MyMultiDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyMultiDataInfo.MyMessage msg) {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        int randomInt = new Random().nextInt(3);

        MyMultiDataInfo.MyMessage myMessage;
        // @formatter:off
        if (0 == randomInt) {
            myMessage =
                MyMultiDataInfo.MyMessage.newBuilder()
                                         .setDataType(MyMultiDataInfo.MyMessage.DataType.PersonType)
                                         .setPerson(
                                             MyMultiDataInfo.Person.newBuilder().setName("人").setAge(20).setAddress("一个地址").build()
                                         ).build();
        } else if (1 == randomInt) {
            myMessage =
                MyMultiDataInfo.MyMessage.newBuilder()
                                         .setDataType(MyMultiDataInfo.MyMessage.DataType.DogType)
                                         .setDog(
                                             MyMultiDataInfo.Dog.newBuilder().setName("狗").setAge(20).build()
                                         ).build();
        } else {
            myMessage =
                MyMultiDataInfo.MyMessage.newBuilder()
                                         .setDataType(MyMultiDataInfo.MyMessage.DataType.CatType)
                                         .setCat(
                                             MyMultiDataInfo.Cat.newBuilder().setName("猫").setCity("一个城市").build()
                                         ).build();
        }

        ctx.channel().writeAndFlush(myMessage);
    }
}
